@echo off

setlocal

set script_dir=%~dp0

set exchange_src_root=%script_dir%..

set build_type=%1

if "%build_type%" == "" (
    set build_type=Release
)

set ini_reader_function=%script_dir%script/win/read_ini_config.bat
set main_ini=%script_dir%config/main.ini
call %ini_reader_function% %main_ini% cmake_bin cmake_bin
call %ini_reader_function% %main_ini% INTEROP_ROOT INTEROP_ROOT
call %ini_reader_function% %main_ini% ACIS_ROOT ACIS_ROOT
call %ini_reader_function% %main_ini% INTEROP_LIBRARY_ZIP_DIR INTEROP_LIBRARY_ZIP_DIR
call %ini_reader_function% %main_ini% DTK_SDK_DIR DTK_SDK_DIR


set PATH=%INTEROP_ROOT%;%ACIS_ROOT%;%DTK_SDK_DIR%;%PATH%
set clone_script_path=%script_dir%script/win/clone.bat

call :build_amcax_interOp

echo "build interOp success"
echo "set INTEROP_BIN_ROOT=%INTEROP_BIN_ROOT%"
echo "set INTEROP_BIN=%INTEROP_BIN%"
@REM @REM TEST 
@REM set INTEROP_BIN_ROOT=D:\workspace\exchange\cad\amcax_exchange\tools\cache\AMCAXInterOp\deploy\install\Release
@REM set INTEROP_BIN=D:\workspace\exchange\cad\amcax_exchange\tools\cache\AMCAXInterOp\deploy\install\Release\bin

if not exist %INTEROP_BIN_ROOT% (
    echo "build interop failed."
    exit /b 0
)

if not exist %INTEROP_BIN% (
    echo "build interop failed."
    exit /b 0
)

call :build_amcax_datakit
@REM set DATAKIT_BIN_ROOT=D:\workspace\exchange\cad\amcax_exchange\tools\cache\AMCAXDatakit\deploy\install\Release
@REM set DATAKIT_BIN=D:\workspace\exchange\cad\amcax_exchange\tools\cache\AMCAXDatakit\deploy\install\Release\bin

if not exist %DATAKIT_BIN_ROOT% (
    echo "build interop failed."
    exit /b 0
)

if not exist %DATAKIT_BIN% (
    echo "build interop failed."
    exit /b 0
)

cd %exchange_src_root%
set build_command=build.bat
call %build_command% "%build_type%"

goto :eof

:build_amcax_interOp
    set interOp_ini=%script_dir%config/interOp.ini
    call %ini_reader_function% %interOp_ini% repo_url repo_url
    call %ini_reader_function% %interOp_ini% branch third_part_repo_tag
    call %ini_reader_function% %interOp_ini% localName third_part_name
    call %ini_reader_function% %interOp_ini% re_clone re_clone
    call %ini_reader_function% %interOp_ini% commit_id commit_id

    call %clone_script_path%
    if NOT DEFINED local_repo_dir (
        echo "clone failed."
        exit /b 0
    )
    cd %local_repo_dir%
    set build_command=build.bat
    
    call %build_command% "%build_type%"
    set INTEROP_BIN_ROOT=%local_repo_dir%/deploy/install/%build_type%
    set INTEROP_BIN=%local_repo_dir%/deploy/install/%build_type%/bin

goto :eof

:build_amcax_datakit
    set datakit_ini=%script_dir%config/datakit.ini
    call %ini_reader_function% %datakit_ini% repo_url repo_url
    call %ini_reader_function% %datakit_ini% branch third_part_repo_tag
    call %ini_reader_function% %datakit_ini% localName third_part_name
    call %ini_reader_function% %datakit_ini% re_clone re_clone
    call %ini_reader_function% %datakit_ini% commit_id commit_id

    call %clone_script_path%
    if NOT DEFINED local_repo_dir (
        echo "clone failed."
        exit /b 0
    )
    cd %local_repo_dir%
    set build_command=build.bat
    
    call %build_command% "%build_type%"
    set DATAKIT_BIN_ROOT=%local_repo_dir%/deploy/install/%build_type%
    set DATAKIT_BIN=%local_repo_dir%/deploy/install/%build_type%/bin

goto :eof



endlocal